<?php

class Default_Model_Yatzy_ResultMapper
{
    protected $_dbTable;



    public function setDbTable($dbTable)
    {
        if (is_string($dbTable)) {
            $dbTable = new $dbTable();
        }
        if (!$dbTable instanceof Zend_Db_Table_Abstract) {
            throw new Exception('Invalid table data gateway provided');
        }
        $this->_dbTable = $dbTable;
        return $this;
    }

    public function getDbTable()
    {
        if (null === $this->_dbTable) {
            $this->setDbTable('Default_Model_Yatzy_DbTable_Result');
        }
        return $this->_dbTable;
    }

    public function save(Default_Model_Yatzy_Result $result)
    {
    	$auth = Zend_Auth::getInstance();
		$userId = $auth->getIdentity()->spagooy_user_id;
    	$data = array(
			'spagooy_result_date'	=>  date('Y-m-d H:i:s'),
//			'spagooy_result_date' => ??????????????,
			'spagooy_result_id' => $result->getId(),
			'spagooy_result_user_id' => $userId,
			'spagooy_result_ones' => $result->getOnes(),
			'spagooy_result_twos' => $result->getTwos(),
			'spagooy_result_threes' => $result->getThrees(),
			'spagooy_result_fours' => $result->getFours(),
			'spagooy_result_fives' => $result->getFives(),
			'spagooy_result_sixes' => $result->getSixes(),
			'spagooy_result_subsum' => $result->getSubsum(),
			'spagooy_result_bonus' => $result->getBonus(),
			'spagooy_result_one_pair' => $result->getPair(),
			'spagooy_result_two_pairs' => $result->getTwoPair(),
			'spagooy_result_three_of_a_kind' => $result->getThreeOfAKind(),
			'spagooy_result_four_of_a_kind' => $result->getFourOfAKind(),
			'spagooy_result_small_straight' => $result->getSmallStraight(),
			'spagooy_result_big_straight' => $result->getBigStraight(),
			'spagooy_result_full_house' => $result->getFullHouse(),
			'spagooy_result_chance' => $result->getChance(),
			'spagooy_result_yatzy' => $result->getYatzy(),
			'spagooy_result_total' => $result->getSum(),
			'spagooy_result_finnished' => $result->getFinnished(),
			'spagooy_result_complete_rounds' => $result->getCompleteRounds(),
			'spagooy_result_complete_die_throws' => $result->getCompleteDiceThrows(),
			'spagooy_result_dice' => $result->getDice()
        );

        if (null === ($id = $result->getId())) {
            unset($data['spagooy_result_id']);
            $ret = $this->getDbTable()->insert($data);
			return $ret;
		} else {
            $this->getDbTable()->update($data, array('spagooy_result_id = ?' => $id));
        }
    }
	
	public function getTopTen() {
        $resultSet = $this->getDbTable()->fetchTopTen();
		$results   = array();
        foreach ($resultSet as $row) {
            $result = new Default_Model_Yatzy_Result();
            $result->setId($row->spagooy_result_id)
                  ->setSum($row->spagooy_result_total)
                  ->setUserId($row->spagooy_result_user_id)
                  ->setMapper($this);
            $results[] = $result;
        }
        return $results;
	}
	
	public function hasUnfinnishedGame($userId) {
        return $this->getDbTable()->hasUnfinnishedGame($userId);
	}
	
	public function getUnfinnishedGame($id) {
        $resultSet = $this->getDbTable()->find($id);
		$results   = array();
        foreach ($resultSet as $row) {
            $result = new Default_Model_Yatzy_Result();
            $result->setId($row->spagooy_result_id)
					->setDate($row->spagooy_result_date)
					->setOnes($row->spagooy_result_ones)
					->setTwos($row->spagooy_result_twos)
					->setThrees($row->spagooy_result_threes)
					->setFours($row->spagooy_result_fours)
					->setFives($row->spagooy_result_fives)
					->setSixes($row->spagooy_result_sixes)
					->setSubSum($row->spagooy_result_subsum)
					->setBonus($row->spagooy_result_bonus)
					->setPair($row->spagooy_result_one_pair)
					->setTwoPair($row->spagooy_result_two_pairs)
					->setThreeOfAKind($row->spagooy_result_three_of_a_kind)
					->setFourOfAKind($row->spagooy_result_four_of_a_kind)
					->setSmallStraight($row->spagooy_result_small_straight)
					->setBigStraight($row->spagooy_result_big_straight)
					->setFullHouse($row->spagooy_result_full_house)
					->setChance($row->spagooy_result_chance)
					->setYatzy($row->spagooy_result_yatzy)
					->setCompleteRounds($row->spagooy_result_complete_rounds)
					->setCompleteDiceThrows($row->spagooy_result_complete_die_throws)
					->setDice($row->spagooy_result_dice)
                  ->setSum($row->spagooy_result_total)
                  ->setUserId($row->spagooy_result_user_id)
				  ->setCompleteRounds($row->spagooy_result_complete_rounds)
				  ->setFinnished($row->spagooy_result_finnished)
                  ->setMapper($this);
            $results[] = $result;
        }
        return $results[0];

	}
/*
    public function find($id, Default_Model_Guestbook $guestbook)
    {
        $result = $this->getDbTable()->find($id);
        if (0 == count($result)) {
            return;
        }
        $row = $result->current();
        $guestbook->setId($row->id)
                  ->setEmail($row->email)
                  ->setComment($row->comment)
                  ->setCreated($row->created);
    }

    public function fetchAll()
    {
        $resultSet = $this->getDbTable()->fetchAll();
        $entries   = array();
        foreach ($resultSet as $row) {
            $entry = new Default_Model_Guestbook();
            $entry->setId($row->id)
                  ->setEmail($row->email)
                  ->setComment($row->comment)
                  ->setCreated($row->created)
                  ->setMapper($this);
            $entries[] = $entry;
        }
        return $entries;
    }
*/

}
